package com.meizi.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.meizi.mall.product.service.AttrGroupService;
import com.meizi.mall.product.service.CategoryService;
import com.meizi.mall.product.vo.AttrGroupWithAttrsVo;
import com.meizi.mall.product.vo.GroupAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.meizi.mall.product.entity.AttrGroupEntity;
import com.meizi.core.tool.api.PageUtils;
import com.meizi.core.tool.api.R;



/**
 * 属性分组
 *
 * @author meizi
 * @email enjoymeizi@gmail.com
 * @date 2021-01-06 13:26:58
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    //Request URL: http://localhost:8100/api/product/attrgroup/225/withattr?t=1610780156716
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId")Long catelogId){

        //1、查出当前分类下的所有属性分组，
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    //path: "/product/attrgroup/1/attr/relation"
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.getRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }
    //? http://localhost:8100/api/product/attrgroup/1/noattr/relation?t=1610367783108&page=1&limit=10&key=
    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }
        /**
         * 列表
         */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable(value = "categoryId") Long categoryId ){
        PageUtils page = attrGroupService.queryPage(params,categoryId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        List<Long> paths =  categoryService.findCatelogPath(attrGroup.getCatelogId());
        GroupAttrVo groupAttrVo = new GroupAttrVo();
        BeanUtils.copyProperties(attrGroup,groupAttrVo);
        groupAttrVo.setCatelogPath(paths);

        return R.ok().put("attrGroup", groupAttrVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
